package top.yuanao.portscan;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/**
 * Main.java
 *
 * @author Yuanao
 * @date 2021/12/21 15:16
 */
public class Main {

    public static volatile JTextArea showText = new JTextArea();

    /**
     * 唯一的窗口文件
     */
    private static final JFrame MAIN_WINDOW = new JFrame("端口扫描器");

    /**
     * 创建线程池
     */
    private static final ThreadPoolExecutor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(1, 12, 60, TimeUnit.MINUTES, new LinkedBlockingQueue<>());


    public static void main(String[] args) {

        MAIN_WINDOW.setSize(500, 500);
        //设置在屏幕中间X
        MAIN_WINDOW.setLocationRelativeTo(null);
        //关闭按钮关闭程序
        MAIN_WINDOW.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //设置头像
        MAIN_WINDOW.setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getClassLoader().getResource("favicon.png")));
        //禁止改变窗口大小
        MAIN_WINDOW.setResizable(false);

        // 创建面板容器，使用默认的布局管理器
        JPanel panel = new JPanel(null);
        panel.setBackground(new Color(255, 255, 255));


        /**
         * 中间显示的文本
         */
        JLabel jLabel = new JLabel();
        jLabel.setText("请输入查询的IP地址:");
        jLabel.setBounds(20, 20, 130, 30);

        panel.add(jLabel);


        JEditorPane jEditorPane = new JEditorPane();
        jEditorPane.setBounds(150, 20, 200, 30);
        jEditorPane.setBackground(new Color(238, 110, 97));
        jEditorPane.setForeground(new Color(255, 255, 255));
        jEditorPane.setFont(new Font(null, Font.BOLD, 16));
        //获取本机的ip地址
        String localIp;
        try {
            localIp = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            localIp = "";
        }
        jEditorPane.setText(localIp);
        panel.add(jEditorPane);


        // 3. 创建一个基本组件（按钮），并添加到 面板容器 中
        JButton btn = new JButton("开始");
        btn.setBounds(370, 20, 60, 30);
        btn.setBackground(new Color(238, 110, 97));
        btn.setForeground(new Color(255, 255, 255));

        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String input = jEditorPane.getText();
                //验证ip地址的正则
                String pattern = "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}";

                boolean matches = Pattern.compile(pattern).matcher(input).matches();
                if (matches) {
                    //是ip地址开始查端口
                    THREAD_POOL_EXECUTOR.execute(() -> {
                        Utils.portScanMultithreading(input, 1, 65535);
                    });
                } else {
                    JOptionPane.showMessageDialog(MAIN_WINDOW, "请输入正确的IP地址", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        panel.add(btn);


        showText.setBackground(new Color(0, 0, 0));
        showText.setForeground(new Color(255, 255, 255));
        showText.setEditable(false);

        JScrollPane jScrollPane = new JScrollPane(showText);
        jScrollPane.setBounds(20, 60, 455, 370);
        jScrollPane.setBackground(new Color(0, 0, 0));

        panel.add(jScrollPane);

        // 把 面板容器 作为窗口的内容面板 设置到 窗口
        MAIN_WINDOW.setContentPane(panel);

        //设置工具栏
        MAIN_WINDOW.setJMenuBar(getMenuBar());
        //显示窗口
        MAIN_WINDOW.setVisible(true);
    }


    /**
     * 获取状态栏对象
     *
     * @return
     */
    private static JMenuBar getMenuBar() {
        /*
         * 创建一个菜单栏
         */
        JMenuBar menuBar = new JMenuBar();
        menuBar.setBackground(new Color(255, 255, 255));

        /*
         * 创建一级菜单
         */
        JMenu fileMenu = new JMenu("文件");
        JMenu editMenu = new JMenu("编辑");
        JMenu viewMenu = new JMenu("视图");
        JMenu aboutMenu = new JMenu("关于");
        // 一级菜单添加到菜单栏
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(viewMenu);
        menuBar.add(aboutMenu);

        /*
         * 创建 "文件" 一级菜单的子菜单
         */
        JMenuItem newMenuItem = new JMenuItem("新建");
        JMenuItem openMenuItem = new JMenuItem("打开");
        JMenuItem exitMenuItem = new JMenuItem("退出");
        // 子菜单添加到一级菜单
        fileMenu.add(newMenuItem);
        fileMenu.add(openMenuItem);
        fileMenu.addSeparator();       // 添加一条分割线
        fileMenu.add(exitMenuItem);


        JMenuItem aboutMenuItem = new JMenuItem("关于本软件");

        aboutMenu.add(aboutMenuItem);




        /*
         * 菜单项的点击/状态改变事件监听，事件监听可以直接设置在具体的子菜单上（这里只设置其中几个）
         */
        // 设置 "新建" 子菜单被点击的监听器
        newMenuItem.addActionListener(e -> JOptionPane.showMessageDialog(MAIN_WINDOW, "别点我", "嗷~~", JOptionPane.ERROR_MESSAGE));
        // 设置 "打开" 子菜单被点击的监听器
        openMenuItem.addActionListener(e -> JOptionPane.showMessageDialog(MAIN_WINDOW, "你烦不烦啊", "嗷~~", JOptionPane.ERROR_MESSAGE));
        // 设置 "退出" 子菜单被点击的监听器
        exitMenuItem.addActionListener(e -> System.exit(1));


        // 设置关于本软件的提示框
        aboutMenuItem.addActionListener(e -> JOptionPane.showMessageDialog(MAIN_WINDOW, "版本: V1.0       ©2021 Yuanao\n主页:https://www.yuanao.top", "关于本软件", JOptionPane.INFORMATION_MESSAGE));

        return menuBar;
    }

}
